<?php
/**
 * 标签服务
 * User: t.young
 * Date: 2018/5/17
 * Time: 17:33
 */

namespace backend\service;


use backend\component\BackendBaseService;
use backend\component\ResultEasyUIFormat;
use backend\component\ResultFormat;
use common\models\info\Tags;
use common\models\info\TagsRelation;

class TagService extends BackendBaseService
{
    /**
     * 获取分类列表信息
     * @param $post
     * @return ResultEasyUIFormat
     */
    public static function tagsList($post) {
        $model = new Tags();
        $table = $model->dataListTable($post);
        return ResultEasyUIFormat::tableFormat($table)->asJson();
    }

    public static function articleRelationList($post) {
        $model = new TagsRelation();
        $table = $model->dataListTableByType($post);
        return ResultEasyUIFormat::tableFormat($table)->asJson();
    }


    /**
     * @param $object_id
     * @param $tag_id
     * @param $type
     */
    public static function artRelieve($object_id,$tag_id,$type) {
        $result = TagsRelation::artRelieve($object_id,$tag_id,$type);
        return ResultFormat::success("解除成功");
    }

    public static function deleteTag($id){
        $model = Tags::findOne($id);
        if (!$model || $model["id"] != $id) {
            return ResultFormat::error("这条数据不存在!");
        }
        try{
            if (!$model->delete()) {
                return ResultFormat::error($model->getErrorsToString());
            }
            return ResultFormat::success("删除成功");
        }catch (\Exception $e) {
            return ResultFormat::error($e->getMessage());
        }
    }
}